﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Net.NetworkInformation;
using Newtonsoft.Json;
using System.IO.IsolatedStorage;

namespace RadarGlobal.UIView
{
    public partial class LoginDlg : PhoneApplicationPage
    {
        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

        Status status = new Status();

        public LoginDlg()
        {
            InitializeComponent();

            if (NetworkInterface.GetIsNetworkAvailable() != true)

                MessageBox.Show("Can't connect database , Please check wifi/cellular data...");

        }
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void TextBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            NavigationService.Navigate(new Uri("/UIView/RegisterDlg.xaml", UriKind.RelativeOrAbsolute));
        }
        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            if (NetworkInterface.GetIsNetworkAvailable() != true)
            {
                MessageBox.Show("Can't connect database , Please check wifi/cellular data...");

                return;
            }
            status.ShowPopup("Please wait! checking connect server...");

            mySQLService.RadarServicePortTypeClient client = new mySQLService.RadarServicePortTypeClient();

            client.CheckLoginCompleted += (s, args) =>
            {
                status.Remove();

                if (args.Result == "false")
                {

                    MessageBox.Show("Please check username or password and try again..!", "Fail login", MessageBoxButton.OK);
                }
               else
                {
                    MessageBox.Show("You login sucess ! welcom to global radar application","Login success!", MessageBoxButton.OK);

                    Global.UserInfo = JsonConvert.DeserializeObject<User>(args.Result);

                    if (settings.Contains("Login"))

                        settings["Login"] = true;

                    else

                        settings.Add("Login", true);

                    NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
                }

            };
            client.CheckLoginAsync(txtUsername.Text, txtPassword.Password.ToString());
        }
    }
    public class User
    {
        public int UserID { set; get; }
        public string Username { set; get; }
        public string Password { set; get; }
        public string Fullname { set; get; }
        public string Email { set; get; }
        public int Permission { set; get; }
        public int Status{ set; get; }
    }
}